﻿using System;
using System.Linq;
using System.Collections.Generic;
using Kiiro.Common.Entities;
using Kiiro.Common.Utility;
using Kiiro.DataAccess;
using Kiiro.ExceptionManagement;
using Kiiro.Validation.Validators.Entities;

namespace Kiiro.Model
{
    public class ActivityHistoryModel
    {
        public static List<ActivityHistory> GetActivity (
            Guid siteCollectionId, string serverRelativeUrl, ItemType itemType, User currentUser, int max, int userId )
        {
            List<ActivityHistory> histories = new List<ActivityHistory> ( );

            bool hasAccess = false;

            if ( SPHelpers.IsPortal ( siteCollectionId, serverRelativeUrl ) )
            {
                hasAccess = 
                    currentUser.Groups.Contains ( KiiroGroup.PortalMember ) ||
                    currentUser.Groups.Contains ( KiiroGroup.PortalOwner );
            }
            else if ( SPHelpers.IsProject ( siteCollectionId, serverRelativeUrl ) )
            {
                hasAccess =
                    currentUser.Groups.Contains ( KiiroGroup.ProjectMember ) ||
                    currentUser.Groups.Contains ( KiiroGroup.ProjectOwner ) ||
                    currentUser.Groups.Contains ( KiiroGroup.PortalOwner );
            }

            if ( hasAccess )
            {
                histories = ActivityHistoryDA.Instance.GetActivity (
                   siteCollectionId, serverRelativeUrl, itemType, max, userId );
                PopulateCreatedBy ( histories, siteCollectionId, serverRelativeUrl );
            }

            return histories;
        }


        private static void PopulateCreatedBy ( List<ActivityHistory> histories, Guid siteCollectionId, string serverRelativeUrl )
        {
            if ( histories != null )
            {
                List<int> userIds = histories.Select<ActivityHistory, int> ( x => x.CreatedBy.Id ).ToList ( );

                if ( userIds != null && userIds.Count > 0 )
                {
                    List<User> users = UserModel.GetUsersByIds ( userIds, siteCollectionId, serverRelativeUrl );

                    foreach ( ActivityHistory activity in histories )
                    {
                        activity.CreatedBy = users.Find ( x => x.Id == activity.CreatedBy.Id );
                    }
                }
            }
        }
        
        public static ActivityHistory Save(ActivityHistory activity, Guid siteCollectionId, string serverRelativeUrl)
        {
            try
            {
                if (activity != null && activity.EditType != EditType.Unchanged)
                {                    
                        new ActivityHistoryValidator().Validate(activity);

                        if (activity.EditType == EditType.Deleted || activity.IsValid())
                        {
                            ActivityHistoryDA.Instance.Save(activity, siteCollectionId, serverRelativeUrl);                            
                        }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex, true);
            }

            return activity;
        }
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.Model/ActivityHistoryModel.cs $
 * 
 * 17    2/08/10 5:04p Camerons
 * 
 * 16    1/25/10 1:17p Camerons
 * 
 * 15    1/12/10 12:03p Camerons
 * 
 * 14    12/04/09 4:13p Shafia
 * 
 * 13    2/12/09 1:26p Shafia
 * 
 * 12    26/11/09 10:35a Camerons
 * 
 * 11    18/11/09 10:53a Camerons
 * 
 * 10    17/11/09 6:46p Camerons
 * 
 * 9     16/11/09 10:33a Shafia
 * added check statment when filtering activity history to check if
 * referenced item is null or not
 * 
 * 8     6/11/09 3:17p Camerons
 * 
 * 7     28/10/09 1:41p Camerons
 * 
 * 6     28/10/09 1:30a Tobys
 * 
 * 5     21/09/09 3:30p Camerons
 * First batch of changes for 1.1.x.x - Security groups update
 * 
 * 4     17/08/09 10:29a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/